﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    /// <summary>
    /// 显示指定品种的年月库存
    /// 若没有则创建
    /// </summary>
    public partial class TSWD02 
    {
        #region ==dwWB04===========
        public static readonly DependencyProperty dwWB04Property = DependencyProperty.Register("dwWB04",
            typeof(DataRow), typeof(TSWD02),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwWB04Changed));
        public DataRow dwWB04
        {
            get
            {
                return (DataRow)GetValue(dwWB04Property);
            }
            set
            {
                SetValue(dwWB04Property, value);
            }
        }
        private static void OndwWB04Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSWD02;
            if (src != null)
            {
                src.dwWB04 = (DataRow)e.NewValue;
                src.Load(); 
            }
        }
        #endregion  
        #region ==dwWD02===========
        public static readonly DependencyProperty dwWD02Property = DependencyProperty.Register("dwWD02",
            typeof(DataRow), typeof(TSWD02),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwWD02Changed));
        public DataRow dwWD02
        {
            get
            {
                return (DataRow)GetValue(dwWD02Property);
            }
            set
            {
                SetValue(dwWD02Property, value);
            }
        }
        private static void OndwWD02Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSWD02;
            if (src != null)
            {
                src.dwWD02 = (DataRow)e.NewValue; 
            }
        }
        #endregion  
        public TSWD02()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWGBST();
            this.DataContext = MV;
            InitializeComponent(); 
        }
         
 
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwWD02 = dwMaster;
        } 
        public void Load()
        { 
            if (dwWB04 == null)
            {
                TBMain.Clear();
                return;
            }  
            dwData = TBMain.NewRow();
            dwData["WGMANDANT"] = dwWB04["WBART"];
            dwData["WGSA"] = dwWB04["WBFARBE"];
            dwData["WGKUNDNR"] = dwWB04["WBLO"];
            dwData["WGWERK"] = dwWB04["WBART"];
            dwData["WGABT"] = dwWB04["WBFARBE"];
            dwData["WGLO"] = dwWB04["WBLO"];
            dwData["WGART"] = dwWB04["WBART"];
            dwData["WGDESS"] = dwWB04["WBFARBE"];
            dwData["WGART"] = dwWB04["WBLO"];
            dwData["WGCOLORIT"] = dwWB04["WBART"];
            dwData["WGFARBE"] = dwWB04["WBFARBE"];
            dwData["WGBESCHICH"] = dwWB04["WBLO"];
            dwData["WGVAR"] = dwWB04["WBART"];
            dwData["WGZUSTAND"] = dwWB04["WBFARBE"];
            dwData["WGZENSUR"] = dwWB04["WBLO"];
            dwData["WGZENSURKZ"] = dwWB04["WBART"];
            dwData["WGLO"] = dwWB04["WBFARBE"];
            dwData["WGMJJ"] = dwWB04["WBLO"];
            dwData["WGMM"] = dwWB04["WBART"]; 
            SResult = ((VWGBST)MV).QueryWGBST(dwData, 2);
            if (SResult.Tables[0].Rows.Count == 0)
            {
                New();
                SResult = ((VWGBST)MV).QueryWGBST(dwData, 2);
            }
            MergeData(SResult,true);
        }

        public override void New()
        {
            base.New();
            dwData = TBMain.NewRow();
            dwData["WGMANDANT"] = dwWB04["WBART"];
            dwData["WGSA"] = dwWB04["WBFARBE"];
            dwData["WGKUNDNR"] = dwWB04["WBLO"];
            dwData["WGWERK"] = dwWB04["WBART"];
            dwData["WGABT"] = dwWB04["WBFARBE"];
            dwData["WGLO"] = dwWB04["WBLO"];
            dwData["WGART"] = dwWB04["WBART"];
            dwData["WGDESS"] = dwWB04["WBFARBE"];
            dwData["WGART"] = dwWB04["WBLO"];
            dwData["WGCOLORIT"] = dwWB04["WBART"];
            dwData["WGFARBE"] = dwWB04["WBFARBE"];
            dwData["WGBESCHICH"] = dwWB04["WBLO"];
            dwData["WGVAR"] = dwWB04["WBART"];
            dwData["WGZUSTAND"] = dwWB04["WBFARBE"];
            dwData["WGZENSUR"] = dwWB04["WBLO"];
            dwData["WGZENSURKZ"] = dwWB04["WBART"];
            dwData["WGLO"] = dwWB04["WBFARBE"];
            dwData["WGMJJ"] = dwWB04["WBLO"];
            dwData["WGMM"] = dwWB04["WBART"];
            TBMain.Rows.InsertAt(dwData, 0);
            vwMaster.View.MoveCurrentToFirst();
            Save();
        }  
        public override void NewCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            DataRow dwData;
            dwData = TBMain.Rows.Add(); 
            vwMaster.View.MoveCurrentToLast();
        }  
        public override void Save()
        {
            base.Save();
            if (dwWD02 == null)
                return; 
            dwData = ((VWGBST)MV).Convert2WGBST(dwWD02, app.company, true);
            ((VWGBST)MV).SaveWGBST(dwData, app.company);
             
        }
        public override void SaveCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            try
            {
                
                Save();
                base.SaveCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
